package com.ruoyi.modules.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.modules.domain.NumRecord;
import com.ruoyi.modules.service.INumRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 次数记录Controller
 *
 * @author ruoyi
 * @date 2022-02-03
 */
@Api(tags = "后台接口 - 次数记录")
@RestController
@RequestMapping("/backstage/numRecord")
public class NumRecordController extends BaseController {
    @Autowired
    private INumRecordService numRecordService;

    /**
     * 查询次数记录列表
     */
    @PreAuthorize("@ss.hasPermi('modules:record:list')")
    @ApiOperation(value="次数记录列表", notes="次数记录列表")
    @GetMapping("/list")
    public TableDataInfo list(NumRecord numRecord) {
        startPage();
        List<NumRecord> list = numRecordService.list(new LambdaQueryWrapper<NumRecord>()
            .eq(NumRecord::getAgentUserId, numRecord.getAgentUserId())
            .eq(numRecord.getPayType() != null, NumRecord::getPayType, numRecord.getPayType())
            .orderByDesc(NumRecord::getCreateTime)
        );
        return getDataTable(list);
    }

    /**
     * 导出次数记录列表
     */
    @PreAuthorize("@ss.hasPermi('modules:record:export')")
    @Log(title = "次数记录", businessType = BusinessType.EXPORT)
    @ApiOperation(value="导出表格", notes="导出表格")
    @GetMapping("/export")
    public AjaxResult export(NumRecord numRecord) {
        List<NumRecord> list = numRecordService.list(new QueryWrapper<>(numRecord));
        ExcelUtil<NumRecord> util = new ExcelUtil<NumRecord>(NumRecord.class);
        return util.exportExcel(list, "次数记录数据");
    }

    /**
     * 获取次数记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('modules:record:query')")
    @ApiOperation(value="次数记录详情", notes="次数记录列情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(numRecordService.getById(id));
    }

    /**
     * 新增次数记录
     */
    @PreAuthorize("@ss.hasPermi('modules:record:add')")
    @Log(title = "次数记录", businessType = BusinessType.INSERT)
    @ApiOperation(value="次数记录新增", notes="次数记录新增")
    @PostMapping
    public AjaxResult add(@RequestBody NumRecord numRecord) {
        return toAjax(numRecordService.save(numRecord));
    }

    /**
     * 修改次数记录
     */
    @PreAuthorize("@ss.hasPermi('modules:record:edit')")
    @Log(title = "次数记录", businessType = BusinessType.UPDATE)
    @ApiOperation(value="次数记录修改", notes="次数记录修改")
    @PutMapping
    public AjaxResult edit(@RequestBody NumRecord numRecord) {
        return toAjax(numRecordService.updateById(numRecord));
    }

    /**
     * 删除次数记录
     */
    @PreAuthorize("@ss.hasPermi('modules:record:remove')")
    @Log(title = "次数记录", businessType = BusinessType.DELETE)
    @ApiOperation(value="次数记录删除", notes="次数记录删除")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(numRecordService.removeByIds(Arrays.asList(ids)));
    }

}
